
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
package be.ac.ua.pats.thesisrobotica.robot.display;

//~--- non-JDK imports --------------------------------------------------------

import lejos.nxt.Button;
import lejos.nxt.ButtonListener;
import lejos.nxt.LCD;
import lejos.nxt.comm.RConsole;

//~--- JDK imports ------------------------------------------------------------

import java.util.ArrayList;

/**
 * An LCD menu with all the currently available options.
 *
 * @author Jimmy
 */
public class Menu {
    public static final int ALL_TEST              = 5;
    public static final int BENCHMARK_TEST        = 4;
    public static final int COLORLIGHTSENSOR_TEST = 3;
    public static final int NOT_SPECIFIED         = 0;
    public static final int TOUCHSENSOR_TEST      = 1;
    public static final int ULTRASONICSENSOR_TEST = 2;
    int                     chosen                = NOT_SPECIFIED;
    int                     currentOption         = 0;
    ArrayList<Integer>      options               = new ArrayList<Integer>();

    public Menu() {
        options = new ArrayList<Integer>();
    }

    public Menu(ArrayList options) {
        this.options = options;
    }

    public void add(int s) {
        options.add(s);
    }

    /**
     * Goes to the next option as currently selected option.
     */
    private void nextOption() {
        if (currentOption < options.size() - 1) {
            currentOption++;
        } else {
            currentOption = 0;
        }
    }

    /**
     * Goes to the previous option as currently selected option.
     */
    private void previousOption() {
        if (currentOption > 0) {
            currentOption--;
        } else {
            currentOption = options.size() - 1;
        }
    }

    /**
     * Refreshes the display to correctly display the newly selected option.
     */
    public void refresh() {
        int option = options.get(currentOption);

        RConsole.println("currentoption: " + option);
        LCD.clearDisplay();

        switch (option) {
        case (TOUCHSENSOR_TEST) :
            RConsole.println("Selected option " + Menu.TOUCHSENSOR_TEST);
            LCD.drawString("Please select:", 0, 0);
            LCD.drawString("TS Test", LCD.CELL_WIDTH / 3, LCD.CELL_HEIGHT / 2);

            break;

        case (ULTRASONICSENSOR_TEST) :
            RConsole.println("Selected option " + Menu.ULTRASONICSENSOR_TEST);
            LCD.drawString("Please select:", 0, 0);
            LCD.drawString("US Test", LCD.CELL_WIDTH / 3, LCD.CELL_HEIGHT / 2);

            break;

        case (COLORLIGHTSENSOR_TEST) :
            RConsole.println("Selected option " + Menu.COLORLIGHTSENSOR_TEST);
            LCD.drawString("Please select:", 0, 0);
            LCD.drawString("CS Test", LCD.CELL_WIDTH / 3, LCD.CELL_HEIGHT / 2);

            break;

        case (BENCHMARK_TEST) :
            RConsole.println("Selected option " + Menu.BENCHMARK_TEST);
            LCD.drawString("Please select:", 0, 0);
            LCD.drawString("Benchmark Test", LCD.CELL_WIDTH / 3, LCD.CELL_HEIGHT / 2);

            break;

        case (ALL_TEST) :
            RConsole.println("Selected option " + Menu.ALL_TEST);
            LCD.drawString("Please select:", 0, 0);
            LCD.drawString("All Test", LCD.CELL_WIDTH / 3, LCD.CELL_HEIGHT / 2);
        }
    }

    /**
     * Initiates the menu by adding the appropriate button listeners.
     *
     * @return The chosen option.
     */
    public int start() {
        RConsole.println("Running Menu");
        refresh();
        RConsole.println("Adding buttonListener LEFT");
        Button.LEFT.addButtonListener(new ButtonListener() {
            public void buttonPressed(Button button) {
                previousOption();
                refresh();
            }
            public void buttonReleased(Button button) {}
        });
        RConsole.println("Adding buttonListener RIGHT");
        Button.RIGHT.addButtonListener(new ButtonListener() {
            public void buttonPressed(Button button) {
                nextOption();
                refresh();
            }
            public void buttonReleased(Button button) {}
        });
        RConsole.println("Adding buttonListener ESCAPE");
        Button.ESCAPE.addButtonListener(new ButtonListener() {
            public void buttonPressed(Button button) {
                RConsole.close();
                System.exit(1);
            }
            public void buttonReleased(Button button) {}
        });
        RConsole.println("Adding buttonListener ENTER");
        Button.ENTER.addButtonListener(new ButtonListener() {
            public void buttonPressed(Button button) {
                chosen = options.get(currentOption);
            }
            public void buttonReleased(Button button) {}
        });

        while (chosen == NOT_SPECIFIED) {}

        return chosen;
    }
}
